package com.qf.medicaldoctoradmin.healthRecord.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.common.entity.User;
import com.qf.common.utils.OssUtils;
import com.qf.common.utils.UserUtils;
import com.qf.medicaldoctoradmin.healthRecord.dao.HealthDao;
import com.qf.medicaldoctoradmin.healthRecord.service.HealthService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2024-11-09 11:14:15
 */
@Service("HealthService")
public class HealthServiceImpl extends ServiceImpl<HealthDao, User> implements HealthService {

    @Resource
    private HealthDao healthDao;

    @Override
    public void update(User user) {
        if (user.getPassword() != null && user.getPhone() != null) {
            healthDao.updateById(user);
        } else {
            throw new IllegalArgumentException("手机号或密码不能为空!");
        }
    }

    /**
     * 上传头像
     * @param file 上传的文件
     * @return 头像路径
     */
    @Override
    public String uploadAvatar(MultipartFile file) {
        Integer id = UserUtils.getUid(); // 从 ThreadLocal 获取用户 ID
        System.out.println("-------上传图片id为-------" + id + "--------------");
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        filename = uuid + filename.substring(filename.lastIndexOf("."));
        String path = null;
        try {
            // 上传文件到OSS
            path = OssUtils.uploadFile2OSS(file.getInputStream(), filename);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建 User 对象并设置用户ID和头像路径
        User user = new User();
        user.setId(id);
        user.setAvatar(path);

        healthDao.updateById(user);

        return path;
    }


}

